package main.www.wonderfuldata.servlet;

import main.www.wonderfuldata.PreparedStatement.PreparedStatementTest;
import main.www.wonderfuldata.bean.bookInfo;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(name = "addBook",urlPatterns = {"/addBook"})
public class addBook extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符编码
        request.setCharacterEncoding("utf8");

        bookInfo book = new bookInfo();


        //从request对象中获取username,password
        //int bookId = request.getIntHeader("bookId"); id自动生成的
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String bookPages = request.getParameter("bookPages");
        String publishingHouse = request.getParameter("publishingHouse");
        String category = request.getParameter("category");



        HttpSession session = request.getSession(true);
        //判断是否为管理员
//        if("administrator".equals(username)&&"123456".equals(password)){
//            //登录成功，设置session
//
//            session.setAttribute("user", "管理员，欢迎你！");
//
//        }else {
//
//            session.setAttribute("user","登录信息错误，请检查用户名或密码");
//            return ;
//        }

        String sql = "insert into bookInfo(bookName,author,bookPages,publishingHouse,category) values(?,?,?,?,?)";


        PreparedStatementTest pt = new PreparedStatementTest();
        pt.update(sql,bookName,author,bookPages,publishingHouse,category);




        //将页面转发到欢迎页面
        RequestDispatcher dispatcher ;

        dispatcher  = request.getRequestDispatcher("/welcome.jsp");
        dispatcher.forward(request,response);

    }

}
